﻿using System;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace ERTesting.Gui.ViewModel.Input
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class NameInputViewModel : ViewModelBase
    {

        #region Public Properties

        /// <summary>
        /// The <see cref="InputValue" /> property's name.
        /// </summary>
        public const string InputValuePropertyName = "InputValue";

        private string _inputValue = "";

        

        /// <summary>
        /// Gets the InputValue property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string InputValue
        {
            get
            {
                return _inputValue;
            }

            set
            {
                if (_inputValue == value)
                {
                    return;
                }

                var oldValue = _inputValue;
                _inputValue = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(InputValuePropertyName);
            }
        }


        /// <summary>
        /// The <see cref="InputQuestion" /> property's name.
        /// </summary>
        public const string InputQuestionPropertyName = "InputQuestion";

        private String _inputQuestion = "Please enter a name:";
        private Action<bool, string> _callback;

        /// <summary>
        /// Gets the InputQuestion property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public String InputQuestion
        {
            get
            {
                return _inputQuestion;
            }

            set
            {
                if (_inputQuestion == value)
                {
                    return;
                }

                _inputQuestion = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(InputQuestionPropertyName);
            }
        }

        #endregion Public Properties

        #region Constructors
       
        public NameInputViewModel(string inputQuestion)
        {
            InputQuestion = inputQuestion;
        }

        #endregion Constructors

        #region Public Methods

        public void GetInput(Action<Boolean, String> callback)
        {
            _callback = callback;
            ViewModelLocator.Instance.Main.OverlayViewModel = this;
        }

        #endregion Public Methods

        #region Commands

        private ICommand _finishInput;
        public ICommand FinishInput
        {
            get
            {

                if(_finishInput == null)
                {
                    _finishInput = new RelayCommand<bool>((cancel)=>
                                                        {
                                                            ViewModelLocator.Instance.Main.OverlayViewModel = null;
                                                            _callback(cancel, InputValue);
                                                        });
                }
                return _finishInput;
            }
        }
        
        #endregion Commands

    }
}